﻿using System;
using System.Collections.Generic;
using System.Dynamic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Xml.Linq;
using System.Data.Entity;
using WeiBo.Models;
using System.Runtime.Remoting.Contexts;
using System.Security.Cryptography.Xml;
using System.Drawing.Drawing2D;
using System.Collections;

namespace WeiBo.Controllers
{
    public class HomeController : Controller
    {
        WeiBoEntities db = new WeiBoEntities();
        //接收ID
        //WBID = System.Web.HttpContext.Current.Session["id"].ToString();
        //上传ID
        //System.Web.HttpContext.Current.Session["id"] = WBID;
        // GET: Home
        /// <summary>
        /// 广场视图显示
        /// </summary>
        /// <param name="WBID"></param>
        /// <returns></returns>
        public ActionResult SquareIndex(string WBID)
        {
            using (WeiBoEntities db = new WeiBoEntities())
            {
                var PD = "GCIndex";
                ViewBag.PD = PD;

                if (WBID == null)
                {
                    WBID = System.Web.HttpContext.Current.Session["id"].ToString();
                }
                var gerenInfo = db.GeRenInfo.FirstOrDefault(u => u.WBID == WBID);
                ViewBag.WBID = WBID;
                ViewBag.GRName = gerenInfo.GRName;
                ViewBag.GRJianJie = gerenInfo.GRJianJie;
                ViewBag.GRImg = gerenInfo.GRImg;
                System.Web.HttpContext.Current.Session["id"] = WBID;
                ViewBag.Weibo= db.Personal.Where(p=>p.WBID== WBID).Count();
                ViewBag.FenSi= db.GuanZhu.Where(f => f.GuanZhuID == WBID).Count();
               
                var userListTest = from g in db.GeRenInfo
                                   join p in db.Personal on g.WBID equals p.WBID 
                                   select new
                                   {
                                       PID = p.PID,
                                       WBID = p.WBID,
                                       name = g.GRName,
                                       jianjie = g.GRJianJie,
                                       img = g.GRImg,
                                       WenBen = p.PWenBen,
                                       TuPian = p.PTuPian,
                                       ShiPian = p.PShiPin,
                                       XiHuan = p.PXinHuan,
                                       ZhuanFa = p.PZhuanFa
                                   };
                var key = Request["key"];
                if (key != null)
                {
                    if (key=="图片")
                    {
                        userListTest = from g in db.GeRenInfo
                                       join p in db.Personal on g.WBID equals p.WBID
                                       where p.PTuPian!=null
                                       select new
                                       {
                                           PID = p.PID,
                                           WBID = p.WBID,
                                           name = g.GRName,
                                           jianjie = g.GRJianJie,
                                           img = g.GRImg,
                                           WenBen = p.PWenBen,
                                           TuPian = p.PTuPian,
                                           ShiPian = p.PShiPin,
                                           XiHuan = p.PXinHuan,
                                           ZhuanFa = p.PZhuanFa
                                       };
                    }else if (key == "视频")
                    {
                        userListTest = from g in db.GeRenInfo
                                       join p in db.Personal on g.WBID equals p.WBID
                                       where p.PShiPin!= null
                                       select new
                                       {
                                           PID = p.PID,
                                           WBID = p.WBID,
                                           name = g.GRName,
                                           jianjie = g.GRJianJie,
                                           img = g.GRImg,
                                           WenBen = p.PWenBen,
                                           TuPian = p.PTuPian,
                                           ShiPian = p.PShiPin,
                                           XiHuan = p.PXinHuan,
                                           ZhuanFa = p.PZhuanFa
                                       };
                    } else
                    {
                        userListTest = from g in db.GeRenInfo
                                       join p in db.Personal on g.WBID equals p.WBID
                                       where g.GRName.Contains(key) || p.PWenBen.Contains(key)
                                       select new
                                       {
                                           PID = p.PID,
                                           WBID = p.WBID,
                                           name = g.GRName,
                                           jianjie = g.GRJianJie,
                                           img = g.GRImg,
                                           WenBen = p.PWenBen,
                                           TuPian = p.PTuPian,
                                           ShiPian = p.PShiPin,
                                           XiHuan = p.PXinHuan,
                                           ZhuanFa = p.PZhuanFa
                                       };
                    }
                    
                }
                List<dynamic> oneList = new List<dynamic>();
                foreach (var item in userListTest.ToList())
                {
                    dynamic MusicList = new ExpandoObject();
                    MusicList.PID = item.PID;
                    MusicList.WBID = item.WBID;
                    MusicList.name = item.name;
                    MusicList.jianjie = item.jianjie;
                    MusicList.img = item.img;
                    MusicList.WenBen = item.WenBen;
                    MusicList.TuPian = item.TuPian;
                    MusicList.ShiPian = item.ShiPian;
                    MusicList.XiHuan = item.XiHuan;
                    MusicList.ZhuanFa = item.ZhuanFa;
                    oneList.Add(MusicList);
                }

                ViewBag.MusicList = oneList;
                return View();
            }
        }

        /// <summary>
        /// 主页视图显示
        /// </summary>
        /// <returns></returns>
        public ActionResult HomeIndex()
        {
            //广场 主页 值传递
            string WBID = System.Web.HttpContext.Current.Session["id"].ToString();
            System.Web.HttpContext.Current.Session["id"] = WBID;

            var gerenInfo = db.GeRenInfo.FirstOrDefault(u => u.WBID == WBID);
            ViewBag.GRImg = gerenInfo.GRImg;
            ViewBag.WBID = gerenInfo.WBID;
            ViewBag.Weibo = db.Personal.Where(p => p.WBID == WBID).Count();
            ViewBag.FenSi = db.GuanZhu.Where(f => f.GuanZhuID == WBID).Count();
            //--------------------------------------------
            var homeListTest = from g in db.GeRenInfo
                               join p in db.Personal on g.WBID equals p.WBID
                               where g.WBID == WBID
                               select new
                               {
                                   PID = p.PID,
                                   name = g.GRName,
                                   jianjie = g.GRJianJie,
                                   img = g.GRImg,
                                   WenBen = p.PWenBen,
                                   TuPian = p.PTuPian,
                                   ShiPian = p.PShiPin,
                                   XiHuan = p.PXinHuan,
                                   ZhuanFa = p.PZhuanFa
                               };
            List<dynamic> oneList = new List<dynamic>();
            foreach (var item in homeListTest.ToList())
            {
                dynamic MusicList = new ExpandoObject();
                MusicList.PID = item.PID;
                MusicList.name = item.name;
                MusicList.jianjie = item.jianjie;
                MusicList.img = item.img;
                MusicList.WenBen = item.WenBen;
                MusicList.TuPian = item.TuPian;
                MusicList.ShiPian = item.ShiPian;
                MusicList.XiHuan = item.XiHuan;
                MusicList.ZhuanFa = item.ZhuanFa;
                oneList.Add(MusicList);
            }
            ViewBag.HomeList = oneList;

            return View();
        }

        /// <summary>
        /// 好友主页
        /// </summary>
        /// <param name="WBID"></param>
        /// <returns></returns>
        public ActionResult FriendsIndex(string WBID)
        {
            try
            {
                var DLWBID = System.Web.HttpContext.Current.Session["id"].ToString();
                var grInfo = db.GeRenInfo.FirstOrDefault(g => g.WBID == WBID);
                ViewBag.WBID = DLWBID;
                ViewBag.GRName = grInfo.GRName;
                ViewBag.GRJianJie = grInfo.GRJianJie;
                ViewBag.GRImg = grInfo.GRImg;
                ViewBag.Weibo = db.Personal.Where(p => p.WBID == WBID).Count();
                ViewBag.FenSi = db.GuanZhu.Where(f => f.GuanZhuID == WBID).Count();

                var friendslist = from g in db.GeRenInfo
                                  join p in db.Personal on g.WBID equals p.WBID
                                  where g.WBID == WBID
                                  select new
                                  {
                                      PID = p.PID,
                                      name = g.GRName,
                                      jianjie = g.GRJianJie,
                                      img = g.GRImg,
                                      wenben = p.PWenBen,
                                      tupian = p.PTuPian,
                                      shipian = p.PShiPin,
                                      xihuan = p.PXinHuan,
                                      zhuanfa = p.PZhuanFa
                                  };

                List<dynamic> onelist = new List<dynamic>();
                foreach (var item in friendslist.ToList())
                {
                    dynamic musiclist = new ExpandoObject();
                    musiclist.PID = item.PID;
                    musiclist.name = item.name;
                    musiclist.jianjie = item.jianjie;
                    musiclist.img = item.img;
                    musiclist.wenben = item.wenben;
                    musiclist.tupian = item.tupian;
                    musiclist.shipian = item.shipian;
                    musiclist.xihuan = item.xihuan;
                    musiclist.zhuanfa = item.zhuanfa;
                    onelist.Add(musiclist);
                }

                ViewBag.homelist = onelist;

                return View();
            }
            catch (Exception ex)
            {

                throw;
            }
        }


        /// <summary>
        /// 随机生成数
        /// </summary>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string SJS(int length = 6)
        {

            var result = new StringBuilder();
            for (var i = 0; i < length; i++)
            {
                var r = new Random(Guid.NewGuid().GetHashCode());
                result.Append(r.Next(0, 10));
            }
            return result.ToString();
        }

        /// <summary>
        /// 广场发布
        /// </summary>
        /// <param name="WenBen"></param>
        /// <param name="MiaoSuTP"></param>
        /// <param name="MiaoSuSP"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Add(string WenBen, string MiaoSuTP, string MiaoSuSP)
        {
            string WBID = System.Web.HttpContext.Current.Session["id"].ToString();
            string wenb = WenBen + MiaoSuTP + MiaoSuSP;
            string id = SJS();
            if (Request.Files.Count > 0)
            {
                //获取文件
                var file = HttpContext.Request.Files[0];
                //获取上传文件的扩展名
                //首先将文件名按.切割
                var temp = file.FileName.Split('.');
                //获取扩展名
                var extendName = temp[temp.Length - 1];
                if (extendName == "jpg" || extendName == "png" || extendName == "jpg" || extendName == "jpeg")
                {
                    //获取路径
                    var path = HttpContext.Server.MapPath("~/TuPian/");
                    //判断路径是否存在
                    if (Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    //文件重新命名
                    var name = DateTime.Now.ToFileTime() + "." + extendName;
                    //保存上传文件
                    file.SaveAs(Path.Combine(path, name));
                    //保存到数据库
                    var Personallist = new Personal() { PID = id, WBID = WBID, PWenBen = wenb, PTuPian = name };
                    db.Personal.Add(Personallist);
                    db.SaveChanges();
                    //返回结果
                    return RedirectToAction("SquareIndex", "Home");
                }
                else if (extendName == "mp4" || extendName == "flv" || extendName == "avi" || extendName == "mov" || extendName == "wmv")
                {
                    //获取路径
                    var path = HttpContext.Server.MapPath("~/ShiPian/");
                    //判断路径是否存在
                    if (Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    //文件重新命名
                    var name = DateTime.Now.ToFileTime() + "." + extendName;
                    //保存上传文件
                    file.SaveAs(Path.Combine(path, name));
                    //保存到数据库
                    var Personallist = new Personal() { PID = id, WBID = WBID, PWenBen = wenb, PShiPin = name };
                    db.Personal.Add(Personallist);
                    db.SaveChanges();
                    //返回结果
                    return RedirectToAction("SquareIndex", "Home");
                }
                else if (wenb != null)
                {
                    //保存到数据库
                    var Personallist = new Personal() { PID = id, WBID = WBID, PWenBen = wenb };
                    db.Personal.Add(Personallist);
                    db.SaveChanges();
                    //返回结果
                    return RedirectToAction("SquareIndex", "Home");
                }
                else
                {
                    return View();
                }

            }
            else
            {
                return View();
            }

        }

        /// <summary>
        /// 主页发布
        /// </summary>
        /// <param name="shurukuang"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult HomeAdd(string shurukuang)
        {
            string WBID = System.Web.HttpContext.Current.Session["id"].ToString();
            string id = SJS();
            if (Request.Files.Count > 0)
            {
                //获取文件
                var file = HttpContext.Request.Files[0];
                //获取上传文件的扩展名
                //首先将文件名按.切割
                var temp = file.FileName.Split('.');
                //获取扩展名
                var extendName = temp[temp.Length - 1];
                if (extendName == "jpg" || extendName == "png" || extendName == "jpg" || extendName == "jpeg")
                {
                    //获取路径
                    var path = HttpContext.Server.MapPath("~/TuPian/");
                    //判断路径是否存在
                    if (Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    //文件重新命名
                    var name = DateTime.Now.ToFileTime() + "." + extendName;
                    //保存上传文件
                    file.SaveAs(Path.Combine(path, name));
                    //保存到数据库
                    var Personallist = new Personal() { PID = id, WBID = WBID, PWenBen = shurukuang, PTuPian = name };
                    db.Personal.Add(Personallist);
                    db.SaveChanges();
                    //返回结果
                    return RedirectToAction("HomeIndex", "Home");
                }
                else if (extendName == "mp4" || extendName == "flv" || extendName == "avi" || extendName == "mov" || extendName == "wmv")
                {
                    //获取路径
                    var path = HttpContext.Server.MapPath("~/ShiPian/");
                    //判断路径是否存在
                    if (Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    //文件重新命名
                    var name = DateTime.Now.ToFileTime() + "." + extendName;
                    //保存上传文件
                    file.SaveAs(Path.Combine(path, name));
                    //保存到数据库
                    var Personallist = new Personal() { PID = id, WBID = WBID, PWenBen = shurukuang, PShiPin = name };
                    db.Personal.Add(Personallist);
                    db.SaveChanges();
                    //返回结果
                    return RedirectToAction("HomeIndex", "Home");
                }
                else if (shurukuang != null)
                {
                    //保存到数据库
                    var Personallist = new Personal() { PID = id, WBID = WBID, PWenBen = shurukuang };
                    db.Personal.Add(Personallist);
                    db.SaveChanges();
                    //返回结果
                    return RedirectToAction("HomeIndex", "Home");
                }
                else
                {
                    return View();
                }

            }
            else
            {
                return View();
            }
        }
        /// <summary>
        /// 主页删除
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Del(string pid)
        {
            var Personallist = db.Personal.FirstOrDefault(p => p.PID == pid);
            var PLlist = db.CommentArea.Where(p => p.PID == pid);
            var tp = Personallist.PTuPian;
            var sp = Personallist.PShiPin;
            if (tp != null)
            {
                var path = HttpContext.Server.MapPath("~/TuPian/");
                System.IO.File.Delete(Path.Combine(path, tp));
            }
            else if (sp != null)
            {
                var path = HttpContext.Server.MapPath("~/ShiPian/");
                System.IO.File.Delete(Path.Combine(path, sp));
            }
            db.Personal.Remove(Personallist);
            db.CommentArea.RemoveRange(PLlist);
            db.SaveChanges();
            return RedirectToAction("HomeIndex", "Home");
        }

        /// <summary>
        /// 广场删除
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SquareDel(string pid)
        {
            var Personallist = db.Personal.FirstOrDefault(p => p.PID == pid);
            var PLlist = db.CommentArea.Where(p => p.PID == pid);
            var tp = Personallist.PTuPian;
            var sp = Personallist.PShiPin;
            if (tp != null)
            {
                var path = HttpContext.Server.MapPath("~/TuPian/");
                System.IO.File.Delete(Path.Combine(path, tp));
            }
            else if (sp != null)
            {
                var path = HttpContext.Server.MapPath("~/ShiPian/");
                System.IO.File.Delete(Path.Combine(path, sp));

            }
            db.Personal.Remove(Personallist);
            db.CommentArea.RemoveRange(PLlist);
            db.SaveChanges();
            return RedirectToAction("SquareIndex", "Home");
        }

        /// <summary>
        /// 异步显示个人信息
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult grSelect(string id)
        {
            //涉及到连表的情况，把需要的数据映射到匿名类中
            var geinfo = db.GeRenInfo.Where(p => p.WBID == id).Select(a => new { GRName = a.GRName, GRJianJie = a.GRJianJie });
            //先将对象转成字符串
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(geinfo);
            //再返回到视图中
            return Json(json);
        }

        /// <summary>
        /// 异步显示好友列表
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult XianShiHouYou(string id)
        {
            using (WeiBoEntities db = new WeiBoEntities())
            {
                var houyouList = from g in db.GuanZhu
                                 join r in db.GeRenInfo on g.GuanZhuID equals r.WBID
                                 where g.WBID == id
                                 select new
                                 {
                                     WBID = r.WBID,
                                     name = r.GRName,
                                     img = r.GRImg

                                 };
                List<dynamic> houyou = new List<dynamic>();
                foreach (var item in houyouList.ToList())
                {
                    dynamic Houyou = new ExpandoObject();
                    Houyou.name = item.name;
                    Houyou.img = item.img;
                    Houyou.WBID = item.WBID;
                    houyou.Add(Houyou);
                }

                //先将对象转成字符串
                string json = Newtonsoft.Json.JsonConvert.SerializeObject(houyou);
                return Json(json);
            }
        }

        /// <summary>
        /// 修改个人信息
        /// </summary>
        /// <param name="GRName"></param>
        /// <param name="GRJianJie"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UpdateGRinfo(string GRName, string GRJianJie)
        {
            string WBID = System.Web.HttpContext.Current.Session["id"].ToString();
            var XiuGai = db.GeRenInfo.FirstOrDefault(p => p.WBID == WBID);
            XiuGai.GRName = GRName;
            XiuGai.GRJianJie = GRJianJie;
            int count = db.SaveChanges();
            var a = false;
            if (count > 0)
            {
                a = true;
                return Json(a);
            }
            return Json(a);
        }

        /// <summary>
        /// 修改头像
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UpdateGRimg()
        {
            string WBID = System.Web.HttpContext.Current.Session["id"].ToString();
            var XiuGai = db.GeRenInfo.FirstOrDefault(p => p.WBID == WBID);
            var a = false;
            //数据库原有图片
            var txImg = XiuGai.GRImg;
            if (Request.Files.Count > 0)
            {
                //获取文件
                var file = Request.Files[0];
                //获取上传文件的扩展名
                //首先将文件名按.切割
                var temp = file.FileName.Split('.');
                //获取扩展名
                var extendName = temp[temp.Length - 1];
                var tpName = temp[0];
                //判断图片是否一样
                if (tpName != txImg)
                {
                    if (extendName == "jpg" || extendName == "png" || extendName == "jpg" || extendName == "jpeg")
                    {
                        //获取路径
                        var path = HttpContext.Server.MapPath("~/TouXiang/");
                        //判断路径是否存在
                        if (Directory.Exists(path))
                        {
                            Directory.CreateDirectory(path);
                        }
                        //文件重新命名
                        var name = DateTime.Now.ToFileTime() + "." + extendName;
                        //保存上传文件
                        file.SaveAs(Path.Combine(path, name));
                        if (txImg != "morentouxiang.jpg")
                        {
                            //清除原本的头像
                            var path1 = HttpContext.Server.MapPath("~/TouXiang/");
                            System.IO.File.Delete(Path.Combine(path1, txImg));
                        }
                        //保存到数据库
                        XiuGai.GRImg = name;
                        int count = db.SaveChanges();
                        if (count > 0)
                        {
                            a = true;
                            return Json(a);
                        }
                    }
                }
            }
            return Json(a);
        }

        /// <summary>
        /// 评论显示
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult PLSelect(string id)
        {

            var userListTest1 = from c in db.CommentArea
                                join g in db.GeRenInfo on c.WBID equals g.WBID
                                where c.PID == id
                                select new
                                {
                                    name = g.GRName,
                                    plnr = c.Comments
                                };
            List<dynamic> oneList1 = new List<dynamic>();
            foreach (var item in userListTest1.ToList())
            {
                dynamic MusicList = new ExpandoObject();
                MusicList.name = item.name;
                MusicList.plnr = item.plnr;
                oneList1.Add(MusicList);
            }

            //先将对象转成字符串
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(oneList1);
            return Json(json);
        }


        /// <summary>
        /// 增加评论
        /// </summary>
        /// <param name="PID"></param>
        /// <param name="WBID"></param>
        /// <param name="addtxt"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult ADDPL(string PID, string addtxt)
        {
            using (WeiBoEntities db = new WeiBoEntities())
            {
                try
                {
                    string WBID = System.Web.HttpContext.Current.Session["id"].ToString();
                    var a = false;
                    if (addtxt == "")
                    {
                        return Json(a);
                    }
                    else
                    {
                        var PLlist = new CommentArea() { PID = PID, WBID = WBID, Comments = addtxt };
                        db.CommentArea.Add(PLlist);
                        int count = db.SaveChanges();
                        if (count > 0)
                        {
                            a = true;
                        }
                        return Json(a);
                    }
                }
                catch (Exception pl)
                {

                    throw;
                }
            }

        }

        /// <summary>
        /// 添加好友
        /// </summary>
        /// <param name="PID"></param>
        /// <param name="WBID"></param>
        /// <param name="addtxt"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddHouYou(string gzID)
        {
            using (WeiBoEntities db = new WeiBoEntities())
            {
                //try
                //{
                string WBID = System.Web.HttpContext.Current.Session["id"].ToString();
                var a = 3;
                //判断是否有好友
                var list = db.GuanZhu.Where(p => p.WBID == WBID).Count();
                //判断好友是否存在
                var list1 = db.GuanZhu.Where(p => p.WBID == WBID && p.GuanZhuID == gzID).Count();
                if (list == 0)
                {
                    var addlist = new GuanZhu() { WBID = WBID, GuanZhuID = gzID };
                    db.GuanZhu.Add(addlist);
                    db.SaveChanges();
                    a = 1;
                }
                else if (list1 == 0)
                {
                    var addlist = new GuanZhu() { WBID = WBID, GuanZhuID = gzID };
                    db.GuanZhu.Add(addlist);
                    db.SaveChanges();
                    a = 1;
                }
                else
                {
                    a = 2;
                }
                return Json(a);
                //}
                //catch (Exception pl)
                //{

                //    throw;
                //}
            }

        }

        /// <summary>
        /// 好友分类表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult FriendFL()
        {
            try
            {
                using (WeiBoEntities db = new WeiBoEntities())
                {
                    var WBID = System.Web.HttpContext.Current.Session["id"].ToString();
                    //涉及到连表的情况，把需要的数据映射到匿名类中
                    var geinfo = db.HouYouFenLei.Where(p => p.WBID == WBID).Select(a => new { flName = a.FLName, flid = a.FLID });
                    //先将对象转成字符串
                    string json = Newtonsoft.Json.JsonConvert.SerializeObject(geinfo);
                    //再返回到视图中
                    return Json(json);
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 好友主页分类显示 
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult FriendSel( string id)
        {
            try
            {
                using (WeiBoEntities db = new WeiBoEntities())
                {
                    string jsonString = string.Empty;
                    var WBID = System.Web.HttpContext.Current.Session["id"].ToString();
                    //涉及到连表的情况，把需要的数据映射到匿名类中
                    var houyouList = from g in db.GuanZhu
                                     join r in db.GeRenInfo on g.GuanZhuID equals r.WBID
                                     where g.WBID == WBID
                                     select new
                                     {
                                         WBID = r.WBID,
                                         name = r.GRName,
                                         img = r.GRImg,
                                         gzid=g.GuanZhuID,
                                     };
                    if (id!="")
                    {
                        houyouList = from g in db.GuanZhu
                                         join r in db.GeRenInfo on g.GuanZhuID equals r.WBID
                                         where g.WBID == WBID && g.FenZuID==id
                                         select new
                                         {
                                             WBID = r.WBID,
                                             name = r.GRName,
                                             img = r.GRImg,
                                             gzid = g.GuanZhuID,
                                         };
                    }
                    List<dynamic> geinfo = new List<dynamic>();
                    foreach (var item in houyouList.ToList())
                    {
                        dynamic Houyou = new ExpandoObject();
                        Houyou.name = item.name;
                        Houyou.img = item.img;
                        Houyou.WBID = item.WBID;
                        Houyou.gzid = item.gzid;
                        geinfo.Add(Houyou);
                    }
                    //分类表
                    var Fenleilist = db.HouYouFenLei.Where(p => p.WBID == WBID).Select(a => new { flName = a.FLName, flid = a.FLID });

                    jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(new
                    {
                        FenLei = Fenleilist,
                        geinfo = geinfo
                    });

                    //再返回到视图中
                    return Json(jsonString);
                }
            }
            catch (Exception)
            {

                throw;
            }
        }


        /// <summary>
        /// 好友主页分类显示 全部
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult FriendSelQB()
        {
            try
            {
                using (WeiBoEntities db = new WeiBoEntities())
                {
                    string jsonString = string.Empty;
                    var WBID = System.Web.HttpContext.Current.Session["id"].ToString();
                    //涉及到连表的情况，把需要的数据映射到匿名类中
                    var houyouList = from g in db.GuanZhu
                                     join r in db.GeRenInfo on g.GuanZhuID equals r.WBID
                                     where g.WBID == WBID
                                     select new
                                     {
                                         WBID = r.WBID,
                                         name = r.GRName,
                                         img = r.GRImg,
                                         gzid = g.GuanZhuID,
                                     };
                    List<dynamic> geinfo = new List<dynamic>();
                    foreach (var item in houyouList.ToList())
                    {
                        dynamic Houyou = new ExpandoObject();
                        Houyou.name = item.name;
                        Houyou.img = item.img;
                        Houyou.WBID = item.WBID;
                        Houyou.gzid = item.gzid;
                        geinfo.Add(Houyou);
                    }
                    //分类表
                    var Fenleilist = db.HouYouFenLei.Where(p => p.WBID == WBID).Select(a => new { flName = a.FLName, flid = a.FLID });

                    jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(new
                    {
                        FenLei = Fenleilist,
                        geinfo = geinfo
                    });

                    //再返回到视图中
                    return Json(jsonString);
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 添加分类
        /// </summary>
        /// <param name="gzID"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddFLtable(string addName)
        {
            using (WeiBoEntities db = new WeiBoEntities())
            {
                //try
                //{
                string id = SJS();
                string WBID = System.Web.HttpContext.Current.Session["id"].ToString();
                var a = false;
                if (addName!="")
                {
                    var addlist = new HouYouFenLei() { WBID = WBID, FLID = id, FLName = addName };
                    db.HouYouFenLei.Add(addlist);
                    int c=db.SaveChanges();
                    if (c>0)
                    {
                        a = true;
                        return Json(a);
                    }
                }
                return Json(a);
                //}
                //catch (Exception pl)
                //{

                //    throw;
                //}
            }

        }

        /// <summary>
        /// 删除分类
        /// </summary>
        /// <param name="gzID"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult DelFLtable(string id)
        {
            using (WeiBoEntities db = new WeiBoEntities())
            {
                try
                {
                    var a = false;
                if (id!="")
                {
                    var Dellist = db.HouYouFenLei.FirstOrDefault(p => p.FLID == id);
                    db.HouYouFenLei.Remove(Dellist);
                    int c=db.SaveChanges();
                    if (c>0)
                    {
                        a = true;
                        return Json(a);
                    }
                }
                return Json(a);
                }
                catch (Exception pl)
                {

                    throw;
                }
            }

        }

        /// <summary>
        /// 删除好友
        /// </summary>
        /// <param name="gzID"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Deltable(string gzid)
        {
            using (WeiBoEntities db = new WeiBoEntities())
            {
                try
                {
                    var a = false;
                if (gzid!="")
                {
                    var Dellist = db.GuanZhu.FirstOrDefault(p => p.GuanZhuID == gzid);
                    db.GuanZhu.Remove(Dellist);
                    int c=db.SaveChanges();
                    if (c>0)
                    {
                        a = true;
                        return Json(a);
                    }
                }
                return Json(a);
                }
                catch (Exception pl)
                {

                    throw;
                }
            }

        }

        /// <summary>
        /// 修改好友
        /// </summary>
        /// <param name="gzID"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UpdateHaoYou(string gzid , string fltxt)
        {
            using (WeiBoEntities db = new WeiBoEntities())
            {
                try
                {
                    string WBID = System.Web.HttpContext.Current.Session["id"].ToString();
                    var a = false;
                if (gzid!="")
                {
                    var Upllist = db.GuanZhu.FirstOrDefault(p => p.GuanZhuID == gzid &&p.WBID==WBID );
                        Upllist.FenZuID = fltxt;
                        int c=db.SaveChanges();
                    if (c>0)
                    {
                        a = true;
                        return Json(a);
                    }
                }
                return Json(a);
                }
                catch (Exception pl)
                {

                    throw;
                }
            }

        }

    }
}